package com.frame.common.tools.result;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

/**
 * Created in com.xfw.mop.common.
 * description
 *
 * @author xieran
 * @date 2019/09/25 16:02
 */
public class ServiceResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private String errCode;

    /**
     * 错误描述
     */
    private String errMsg;

    /**
     * 返回值
     */
    private T payload;

    /**
     * 屏蔽静态方法
     */
    private ServiceResult() {

    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }


    /**
     * isSucceed
     *
     * @return boolean
     */
    public boolean isSucceed() {
        return ResultCode.SUCCESS.getCode().equals(this.errCode);
    }


    /**
     * getSuccessResult
     *
     * @param payload payload
     * @param <T>     <T>
     * @return ServiceResult<T>
     */
    public static <T> ServiceResult<T> getSuccessResult(T payload) {
        ServiceResult<T> result = new ServiceResult<>();
        result.setErrCode(ResultCode.SUCCESS.getCode());
        result.setErrMsg(null);
        result.setPayload(payload);
        return result;
    }

    /**
     * getSuccessResult
     *
     * @param <T> <T>
     * @return ServiceResult<T>
     */
    public static <T> ServiceResult<T> getSuccessResult() {
        ServiceResult<T> result = new ServiceResult<>();
        result.setErrCode(ResultCode.SUCCESS.getCode());
        result.setErrMsg(null);
        result.setPayload(null);
        return result;
    }

    /**
     * getFailureResult
     *
     * @param errCode errCode
     * @param errMsg  errMsg
     * @param <T>     <T>
     * @return ServiceResult<T>
     */
    public static <T> ServiceResult<T> getFailureResult(String errCode, String errMsg) {
        ServiceResult<T> result = new ServiceResult<>();
        result.setErrCode(errCode);
        result.setErrMsg(errMsg);
        return result;
    }

    /**
     * getFailureResult
     *
     * @param resultCode errCode
     * @param <T>        <T>
     * @return ServiceResult<T>
     */
    public static <T> ServiceResult<T> getFailureResult(ResultCode resultCode) {
        ServiceResult<T> result = new ServiceResult<>();
        result.setErrCode(resultCode.getCode());
        result.setErrMsg(resultCode.getContent());
        return result;
    }

    /**
     * getFailureResult
     *
     * @param <T> <T>
     * @return ServiceResult<T>
     */
    public static <T> ServiceResult<T> getFailureResult() {
        ServiceResult<T> result = new ServiceResult<>();
        result.setErrCode(ResultCode.ERROR.getCode());
        result.setErrMsg(ResultCode.ERROR.getContent());
        return result;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue);
    }
}
